package timing.ukulele.portal.util;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import timing.ukulele.common.util.TreeUtil;
import timing.ukulele.data.portal.data.AntMenuTree;
import timing.ukulele.data.portal.view.AntIconVO;
import timing.ukulele.data.portal.view.AntMenuTreeVO;
import timing.ukulele.data.portal.view.AntMenuVO;
import timing.ukulele.portal.convertor.IconConverter;
import timing.ukulele.portal.persistent.AntIcon;
import timing.ukulele.portal.persistent.AntMenu;
import timing.ukulele.portal.service.AntIconService;

import java.util.*;

public final class MenuUtil {
    public static List<AntMenuTree> createMenuTree(Collection<AntMenu> menuList, AntIconService antIconService) {
        List<AntMenuTree> menuTreeList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menuList)) {
            menuList.forEach(menu -> {
                AntMenuTree node = new AntMenuTree();
                BeanUtils.copyProperties(menu, node);
                if (StringUtils.hasText(menu.getAcl())) {
                    node.setAcl(JSON.parseArray(menu.getAcl(), String.class));
                }
                if (menu.getIconId() != null) {
                    AntIcon icon = antIconService.getById(menu.getIconId());
                    AntIconVO antIconVO = new AntIconVO();
                    BeanUtils.copyProperties(icon, antIconVO);
                    node.setIcon(antIconVO);
                }
                menuTreeList.add(node);
            });
        }
        return TreeUtil.buildByRecursive(menuTreeList, 0L);
    }

    public static List<AntMenuTreeVO> createMenuTreeVO(Collection<AntMenu> menuList, AntIconService antIconService) {
        List<AntMenuTreeVO> menuTreeList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menuList)) {
            List<AntIcon> iconList = antIconService.list();
            Map<Long, AntIconVO> iconVOMap = null;
            if (!CollectionUtils.isEmpty(iconList)) {
                iconVOMap = new HashMap<>(iconList.size() * 2);
                Map<Long, AntIconVO> finalIconVOMap = iconVOMap;
                iconList.forEach(icon -> {
                    AntIconVO vo = IconConverter.INSTANCE.p2v(icon);
                    finalIconVOMap.put(icon.getId(), vo);
                });
            }
            Map<Long, AntIconVO> finalIconVOMap = iconVOMap;
            menuList.forEach(menu -> {
                AntMenuTreeVO node = new AntMenuTreeVO();
                AntMenuVO vo = new AntMenuVO();
                BeanUtils.copyProperties(menu, vo);
                node.setMenu(vo);
                node.setKey(vo.getKey());
                node.setTitle(vo.getText());
                node.setId(vo.getId());
                node.setParentId(vo.getParentId());
                node.setDisabled(vo.getDeleted());
                if (StringUtils.hasText(menu.getAcl())) {
                    vo.setAcl(JSON.parseArray(menu.getAcl(), String.class));
                }
                if (menu.getIconId() != null && !CollectionUtils.isEmpty(finalIconVOMap)) {
                    vo.setAntIcon(finalIconVOMap.get(menu.getIconId()));
                }
                menuTreeList.add(node);
            });
        }
        return TreeUtil.buildByRecursive(menuTreeList, 0L);
    }
}
